import { customTable } from '@/components/ProTable/interface';
import { TreeDataProp, useTreeData } from './treeData';
import { useAddForm } from './addForm';
import { Dictionary } from '@/api/interface/basic/dictionary';
import { ProTableValue } from '@/interface/public';

export function useDataGrid(dataGridRef: ProTableValue) {
  const { treeData, searchTreeData, clearTreeData, selectTreeData, findPidByCode } = useTreeData();
  const { addForm, saveAddForm } = useAddForm(dataGridRef);
  let dataGrid = reactive<customTable>({
    initParam: {
      catalogCode: ''
    },
    data: [],
    // 表格配置项
    columns: [
      { prop: 'operation', label: 'operation', fixed: 'left', width: 180 },

      {
        prop: 'dicValue',
        label: '字典值'
      },
      {
        prop: 'paramMark',
        label: '参数值'
      },
      {
        prop: 'remark',
        label: '备注'
      },
      {
        prop: 'showOrder',
        label: '排序'
      },

      { prop: 'remark', label: 'remark' },
      {
        prop: 'dicCode',
        label: '字典码'
      }
    ],
    headButtonList: [
      {
        label: 'add',
        type: 'primary',
        icon: 'Plus',
        // permissionCode: "dictionary:save",
        methods: async () => {
          if (!treeData.value.selectValue) {
            elMessage('请先选择左侧需要新增的明细数据', 'warning');
            return;
          }
          formChangeItemStatus(addForm, ['dicValue'], false);

          const params = {
            dicCode: treeData.value.selectValue,
            dicValue: '',
            pid: findPidByCode(treeData.value.selectValue),
            catalogId: treeData.value.selectId
          };

          formOpen<Dictionary.ReqDoSave>(addForm, 'add', params);
        }
      }
    ],
    rowButtonList: [
      {
        label: 'edit',
        type: 'primary',
        // permissionCode: "dictionary:save",
        methods: async (row: Dictionary.ReqDoSave) => {
          formChangeItemStatus(addForm, ['dicValue'], true);
          formOpen(addForm, '编辑字典', row);
        },
        attrs: {
          link: true
        }
      },
      {
        label: 'enable',
        type: 'primary',
        // permissionCode: "dictionary:enable",
        display: item => item.status === 1,
        methods: row => {
          const params = { id: row.id };
          useHandleData(
            dictEnable,
            params,
            `${transitionLanguages('table', 'enable')}【${row.dicValue}】?`,
            dataGridRef.value.getTableList
          );
        },
        attrs: {
          link: true
        }
      },
      {
        label: 'disabled',
        // permissionCode: "dictionary:disabled",
        type: 'danger',
        display: item => item.status === 0,
        methods: row => {
          const params = { id: row.id, status: 1 };
          useHandleData(
            dictDisable,
            params,
            `${transitionLanguages('table', 'enable')}【${row.dicValue}】?`,
            dataGridRef.value.getTableList
          );
        },
        attrs: {
          link: true
        }
      }
      // {
      //   label: 'delete',
      //   type: 'danger',
      //   // permissionCode: "dictionary:remove",
      //   display: row => row.status === 0,
      //   methods: (row: Dictionary.ResListByCodesProp) => {
      //     const name = `${transitionLanguages('button', 'delete')}【${row.dcode}】?`;
      //     useHandleData(dictionaryDoRemove, { id: row.id }, name, dataGridRef.value.getTableList);
      //   },
      //   attrs: {
      //     link: true
      //   }
      // }
    ]
  });

  const dataGridCallBack = data => {
    console.log(dataGrid.initParam.catalogCode, 'dataGrid.initParam.catalogCode');
    console.log(data, 'data', data[dataGrid.initParam.catalogCode]);

    return data[dataGrid.initParam.catalogCode];
  };

  return {
    dataGrid,
    saveAddForm,
    addForm,
    treeData,
    dataGridCallBack,
    searchTreeData,
    clearTreeData,
    selectTreeData
  };
}
